package com.gang.mars.basic.parseannotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author：gang.chen
 * @Date：2023/5/6 13:33
 */
final class GAttributeMethod {

    private final Method[] methods;

    private final Class<? extends Annotation> annotationType;


    GAttributeMethod(Class<? extends Annotation> annotationType, Method[] methods) {
        this.annotationType = annotationType;
        this.methods = methods;
    }

    public static GAttributeMethod compute(Class<? extends Annotation> annotation)
    {
        Method[] method = annotation.getDeclaredMethods();

        Arrays.sort(method, methodComparator);

        return new GAttributeMethod(annotation, method);
    }


    public int size(){
        return methods.length;
    }

    private static final Comparator<Method> methodComparator = (m1, m2) ->{

        if (m1 != null && m2 != null){
            return m1.getName().compareTo(m2.getName());
        }

        return m1 != null ? -1: 1;
    };

    public Method get(int index)
    {
        return this.methods[index];
    }

}
